package com.thinkgem.jeesite.modules.data.rep.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.data.rep.entity.TfEcReportConfigIn;
import com.thinkgem.jeesite.modules.data.rep.service.ReportConfService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 报表配置输入参数controller
 * @Author havel
 */
@Controller
@RequestMapping(value = "${adminPath}/report/configIn")
public class ConfigInController extends BaseController {

    @Autowired
    ReportConfService reportConfService;


    @RequiresPermissions("report:configIn:view")
    @RequestMapping(value = {"list", ""})
    public String list(@RequestParam long reportId, TfEcReportConfigIn tfEcReportConfigIn, HttpServletRequest request, HttpServletResponse response, Model model) {
        tfEcReportConfigIn.setReportId(reportId);
        Page<TfEcReportConfigIn> page = tfEcReportConfigIn.getPage();
        page.setPageNo(getPageNo(request, response));
        page.setPageSize(getPageSize(request, response, 10));
        page = reportConfService.findPage(page, tfEcReportConfigIn);

        model.addAttribute("page", page);
        model.addAttribute("reportId", reportId);
        return "modules/data/rep/reportConfigInList";
    }

    @RequiresPermissions("report:configIn:edit")
    @RequestMapping(value = "form")
    public String form(String configInId, @RequestParam long reportId, Model model) {
        TfEcReportConfigIn configIn = new TfEcReportConfigIn();
        if (!StringUtils.isEmpty(configInId)) {
            configIn = reportConfService.getConfigInById(Long.parseLong(configInId));
        }
        model.addAttribute("reportConfigIn", configIn);
        model.addAttribute("reportId", reportId);
        return "modules/data/rep/reportConfigInForm";
    }

    @RequiresPermissions("report:configIn:edit")
    @RequestMapping(value = "save")
    public String save(TfEcReportConfigIn reportConfigIn, @RequestParam long reportId, Model model, RedirectAttributes redirectAttributes) {
        if (beanValidator(model, reportConfigIn)) {
            User currentUser = UserUtils.getUser();
            reportConfigIn.setUpdateStaffId(currentUser.getPhone());
            reportConfigIn.setReportId(reportId);
            reportConfService.saveAndUpdate(reportConfigIn);
            this.addMessage(redirectAttributes, "保存" + StringUtils.abbr(reportConfigIn.getItemName(), 50) + "成功");
        } else {
            return form(reportConfigIn.getInConditionId()+"", reportId, model);
        }
        return "redirect:" + adminPath + "/report/configIn?reportId=" + reportId;
    }

    @RequiresPermissions("report:configIn:edit")
    @RequestMapping(value = "delete")
    public String delete(@RequestParam long configInId, @RequestParam long reportId, Model model, RedirectAttributes redirectAttributes) {
        TfEcReportConfigIn reportConfigIn = new TfEcReportConfigIn();
        reportConfigIn.setInConditionId(configInId);
        reportConfService.delete(reportConfigIn);
        this.addMessage(redirectAttributes, "删除" + StringUtils.abbr(reportConfigIn.getItemName(), 50) + "成功");
        return "redirect:" + adminPath + "/report/configIn?reportId=" + reportId;
    }

}
